var burl;
var imageBurl;
var testurl;
var sbl = {
	ProessId:"",
	ajax : function(options){
		
		var url;
		var type;
		
		if(options.type == null || options.type == ""){
			type = "POST";
		}
		
		if(options.url.indexOf("?", 0) != -1)
		{
			url = options.url + "&is_ajax=true";
		}
		else
		{
			url = options.url + "?is_ajax=true";
		}
		$.ajax({
			url:""+url,
			data:typeof(options.data) == "undefined" ?{}:options.data,
			async:typeof(options.async) == "undefined" ?true:false,
			dataType:typeof(options.dataType) == "undefined" ?"json":options.dataType,
			type:type,
			success:function(d)
			{
				if(d.code == "1"||d.code == null)
				{
					if(options.success)
					{
						options.success(d);
					}		
				}
				else if(d.code == '4'){
					alert("获取失败。");
				}
				else if(d.code == '3')
				{
					alert(d.msg);
				}
			}
		});
	},
	submit:function(options)
	{
		var url;
		var fromId = options.data;
		
		if(options.url.indexOf("?", 0) != -1)
		{
			url = ""+options.url + "&is_ajax=true&iframe=1";
		}
		else
		{
			url = ""+options.url + "?is_ajax=true&iframe=1";
		}
		
		$("#upload").remove();
		$("html").append('<iframe id="upload" name="upload" style="display:none"></iframe> ');
		
		$("#"+fromId).attr("enctype","multipart/form-data");
		$("#"+fromId).attr("method","post");
		$("#"+fromId).attr("target","upload");
		$("#"+fromId).attr("action",url);
		$("#"+fromId).submit();
		
		var timevalId = setInterval(function(){
			var result = $("#upload").contents().find("body").text();
			if(result != "")
			{
				clearInterval(timevalId);
				try {
					var d = $.parseJSON(result);
					
					if(d.serverStatus == "2")
					{
						var message = d.Data;
						if(options.error)
						{
							options.error(message);
						}
						else
						{
							alert(message);
						}					
					}
					else if(d.serverStatus == "3")
					{
						window.location.href = ""+"/Home/index";
					}
					else
					{
						if(options.success)
						{
							options.success(d);
						}
					}		
					
				} catch (e) {
					alert(result);
				}
			}
		},50);
	},
	getHtml:function(jObj){
		var div = $("<div></div>").append(jObj);
		return div.html();
	},
	getFormData:function(formId,isget)
	{
		var inputs = $("input[name]",$("#" + formId));
		var radioInput = $("input[type='radio']:checked",$("#" + formId));
		var checkboxInput = $("input[type='checkbox']:checked",$("#" + formId));
		var selects = $("select[name]",$("#" + formId));
		var textarea = $("textarea[name]",$("#" + formId));
		var data = {};
		for(var i = 0 ;i < inputs.length; i++)
		{
			var inputType=$(inputs[i]).attr("type");
			if(inputType=="radio" || inputType == "checkbox")
			{
				continue;
			}
			var name = $(inputs[i]).attr("name");
			if(name != "")
			{
				data[name] = $(inputs[i]).val();
			}
		}
		
		for(var i = 0 ;i < radioInput.length; i++)
		{
			var name = $(radioInput[i]).attr("name");
			if(name != "")
			{
				data[name] = $(radioInput[i]).val();
			}
		}
		
		for(var i = 0 ;i < checkboxInput.length; i++)
		{
			var name = $(checkboxInput[i]).attr("name");
			if(name != "")
			{
				data[name] = $(checkboxInput[i]).val();
			}
		}
		
		for(var i = 0 ;i < selects.length; i++)
		{
			var name = $(selects[i]).attr("name");
			if(name != "")
			{
				data[name] = $(selects[i]).val();
			}
		}
		
		for(var i = 0 ;i < textarea.length; i++)
		{
			var name = $(textarea[i]).attr("name");
			if(name != "")
			{
				data[name] = $(textarea[i]).val();
			}
		}
		
		if(isget){
			var result = "";
			for(var key in data){
				result += key+"="+data[key]+"&";
			}
			return result;
		}
		return data;
	},
	gotoUrl:function(url,isblank)
	{
		if(isblank){
			window.open(url);
		}
		else{
			window.location.href = ""+url;
		}
	},
	mergeObjects:function(targetObject,object,Override){
		for(var key in object)
		{
			var deal = false;
			for(var targetKey in targetObject)
			{
				if(key == targetKey)
				{
					if(targetObject[targetKey] && Override)
					{
						targetObject[targetKey] = object[key];
						break;
					}
					deal = true;
				}				
			}
			if(!deal)
			{
				targetObject[key] = object[key];
			}
		}
		return targetObject;
	},
	dialog:function(options){
		
		var me = this;
		var width = options.width;
		var height = options.height;
		var bodyWidth = $("body").width();
		var contentDivId = options.contentDivId;
		
		var left = (bodyWidth - width)/2 + "px";
		
		var dialogContainerDiv = $("<div id='lrq_dialog_container_div' class='lrq_dialog_container_div'></div>");
		var dialogTopDiv =  $("<div id='lrq_dialog_top_div' class='lrq_dialog_top_div' style='height: 12px;padding-bottom: 4px;'></div>");
		dialogTopDiv.append("<img id='dialog_close_img' style='float: right;cursor: pointer;padding-bottom: 10px;' src='"+burl+"/images/main-images/close.png'/>");
		var dialogMarker = $("<div id='lrq_dialog_marker_div' class='lrq_dialog_marker_div'></div>");
		var dialogDiv = $("<div id='lrq_dialog_div' class='lrq_dialog_div'></div>").css({width:width,height:height,left:left});
		dialogDiv.append(dialogTopDiv);
		dialogContainerDiv.append(dialogMarker);
		dialogContainerDiv.append(dialogDiv);
		
		if(options.url != null && options.url != ""){
			me.ajax({
				url:options.url,
				data:options.data,
				async:false,
				dataType:"text",
				success:function(d){
					if(contentDivId != null && contentDivId != ""){
						var contentDiv = $("<div id='"+contentDivId+"'></div>");
						contentDiv.append($(d));
						dialogDiv.append(contentDiv);
					}
					else{
						dialogDiv.append($(d));
					}
					
					$("body").append(dialogContainerDiv);
					$("#dialog_close_img").bind("click",function(){
						dialogContainerDiv.remove();
					});
					if(options.OnComplete){
						options.OnComplete();
					}
				}
			});
		}
		else{
			var contentDiv = $("<div id='"+contentDivId+"'></div>");
			dialogDiv.append(contentDiv);
			$("body").append(dialogContainerDiv);
			$("#dialog_close_img").bind("click",function(){
				dialogContainerDiv.remove();
			});
			if(options.OnComplete){
				options.OnComplete();
			}
		}
		
		return dialogContainerDiv;
	},
	showProess:function(text){
		var me = this;
		me.removeProess();
		var width = 190;
		var height = 1;
		var bodyWidth = $("body").width();
		var bodyHeight = $("body").height();
		
		var left = (bodyWidth - width)/2 + "px";
		
		var ProessContainerDiv = $("<div id='lrq_process_container_div' style='height:"+bodyHeight+"px' class='lrq_dialog_container_div'></div>");
		var ProessMarker = $("<div id='lrq_dialog_marker_div' class='lrq_dialog_marker_div'></div>");
		var ProessDiv = $("<div id='lrq_dialog_div' style='padding:0px;background-color: black;filter:alpha(opacity=70);opacity: 0.7;border:0px' class='lrq_dialog_div'></div>").css({width:width,height:height,left:left,position:"fixed",top:"200px"});
		var textP = $("<p></p>").html(text).css({color:"#fff","font-weight":"bold"});
		var img = $("<img src ="+burl+"'/images/loading.gif?333'/>");
		ProessContainerDiv.append(ProessMarker);
		ProessContainerDiv.append(ProessDiv.append(textP).append(img));
		
		$("body").append(ProessContainerDiv);
	},
	removeProess:function(){
		$("#lrq_process_container_div").remove();
	},
	getFieldValue : function(value,num){
		if(value == null){
			if(num){
				return 0;
			}
			else{
				return "";
			}
		} else {
			return value;
		}
	},
	loadAddress:function(x,y,domid,isInput){
		
		var gc = new BMap.Geocoder(); 
		var point = new BMap.Point(x,y);
		
		 gc.getLocation(point, function(rs){				 	
	        var address = rs.address;
	        if(isInput){
	        	 $("#"+domid).val(address);
	        }
	        else{
	        	 $("#"+domid).html(address);
	        }
		 });     
	},
	getParameter: function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg);
		if (r != null) return unescape(r[2]);
		return null;
    },
	getBaseUrl: function(){
		var url = $("#BaseUrl").val();
		return url;
	},
	getContextBaseUrl: function(){
		var url = $("#base_url").val();
		return url;
	},
	getImageBaseUrl: function(){
		var url = $("#BaseUrl").val();
		return url;
	},
	clone:function(obj){
		var o;  
	    switch(typeof obj){  
	    case 'undefined': break;  
	    case 'string'   : o = obj + '';break;  
	    case 'number'   : o = obj - 0;break;  
	    case 'boolean'  : o = obj;break;  
	    case 'object'   :  
	        if(obj === null){  
	            o = null;  
	        }else{  
	            if(obj instanceof Array){  
	                o = [];  
	                for(var i = 0, len = obj.length; i < len; i++){  
	                    o.push(sbl.clone(obj[i]));  
	                }  
	            }else{  
	                o = {};  
	                for(var k in obj){  
	                    o[k] = sbl.clone(obj[k]);  
	                }  
	            }  
	        }  
	        break;  
	    default:          
	        o = obj;break;  
	    }  
	    return o;
	}
};
$(function(){
	imageBurl = sbl.getImageBaseUrl();
	burl = sbl.getContextBaseUrl();
});